from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()

class CharacterAnalysis(db.Model):
    """性格分析表"""
    __tablename__ = 'character_analysis'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='分析ID')
    character_id = db.Column(db.String(32), unique=True, nullable=False, comment='性格ID')
    bazi_id = db.Column(db.String(32), db.ForeignKey('bazi_record.bazi_id'), nullable=False, comment='八字ID')
    user_id = db.Column(db.BigInteger, db.ForeignKey('user.id'), nullable=True, comment='用户ID')
    basic_summary = db.Column(db.Text, nullable=True, comment='基础性格概述')
    strengths = db.Column(db.Text, nullable=True, comment='性格优点')
    weaknesses = db.Column(db.Text, nullable=True, comment='性格缺点')
    relationship_style = db.Column(db.Text, nullable=True, comment='人际关系风格')
    work_style = db.Column(db.Text, nullable=True, comment='工作学习风格')
    improvement_suggestion = db.Column(db.Text, nullable=True, comment='性格优化建议')
    is_paid = db.Column(db.SmallInteger, nullable=False, default=0, comment='是否已付费查看')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    user = db.relationship('User', backref=db.backref('character_analyses', lazy='dynamic'))
    bazi_record = db.relationship('BaziRecord', backref=db.backref('character_analyses', lazy='dynamic'))

    def to_dict(self, include_paid_content=False):
        result = {
            'id': self.id,
            'character_id': self.character_id,
            'bazi_id': self.bazi_id,
            'user_id': self.user_id,
            'basic_summary': self.basic_summary,
            'is_paid': self.is_paid,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        }
        
        if include_paid_content or self.is_paid:
            result.update({
                'strengths': self.strengths,
                'weaknesses': self.weaknesses,
                'relationship_style': self.relationship_style,
                'work_style': self.work_style,
                'improvement_suggestion': self.improvement_suggestion
            })
            
        return result


class FortuneForcast(db.Model):
    """运势预测表"""
    __tablename__ = 'fortune_forecast'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='预测ID')
    forecast_id = db.Column(db.String(32), unique=True, nullable=False, comment='预测ID')
    bazi_id = db.Column(db.String(32), db.ForeignKey('bazi_record.bazi_id'), nullable=False, comment='八字ID')
    user_id = db.Column(db.BigInteger, db.ForeignKey('user.id'), nullable=True, comment='用户ID')
    forecast_date = db.Column(db.Date, nullable=False, comment='预测日期')
    forecast_type = db.Column(db.SmallInteger, nullable=False, comment='预测类型：1-日，2-周，3-月，4-年')
    overall_score = db.Column(db.Integer, nullable=False, comment='总体运势评分')
    career_score = db.Column(db.Integer, nullable=False, comment='事业运势评分')
    wealth_score = db.Column(db.Integer, nullable=False, comment='财运评分')
    love_score = db.Column(db.Integer, nullable=False, comment='感情运势评分')
    health_score = db.Column(db.Integer, nullable=False, comment='健康运势评分')
    overall_desc = db.Column(db.Text, nullable=True, comment='总体运势描述')
    career_desc = db.Column(db.Text, nullable=True, comment='事业运势描述')
    wealth_desc = db.Column(db.Text, nullable=True, comment='财运描述')
    love_desc = db.Column(db.Text, nullable=True, comment='感情运势描述')
    health_desc = db.Column(db.Text, nullable=True, comment='健康运势描述')
    suggestion = db.Column(db.Text, nullable=True, comment='行动建议')
    is_paid = db.Column(db.SmallInteger, nullable=False, default=0, comment='是否已付费查看')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    user = db.relationship('User', backref=db.backref('fortune_forecasts', lazy='dynamic'))
    bazi_record = db.relationship('BaziRecord', backref=db.backref('fortune_forecasts', lazy='dynamic'))

    def to_dict(self, include_paid_content=False):
        result = {
            'id': self.id,
            'forecast_id': self.forecast_id,
            'bazi_id': self.bazi_id,
            'user_id': self.user_id,
            'forecast_date': self.forecast_date.strftime('%Y-%m-%d'),
            'forecast_type': self.forecast_type,
            'overall_score': self.overall_score,
            'career_score': self.career_score,
            'wealth_score': self.wealth_score,
            'love_score': self.love_score,
            'health_score': self.health_score,
            'is_paid': self.is_paid,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        }
        
        if include_paid_content or self.is_paid:
            result.update({
                'overall_desc': self.overall_desc,
                'career_desc': self.career_desc,
                'wealth_desc': self.wealth_desc,
                'love_desc': self.love_desc,
                'health_desc': self.health_desc,
                'suggestion': self.suggestion
            })
            
        return result
